<?php
header("Content-Type:text/html;charset=utf-8");
include_once("conn.php");
include ('class-phpmailer.php');
include ('class-smtp.php');

$username1=$_POST["username"];
$password1=$_POST["password1"];
$password2=$_POST["password2"];
$email1=$_POST["email"];


function Check_Username($username)
{
	$Max_Strlen_UserName=16;
	$Min_Strlen_UserName=6;
	$UserNameChars="^[a-zA-Z0-9_-]";
	$UserNameGood="用户名正确";
	if($username=="")
	{
		$UserNameGood="用户名不能为空";
		return $UserNameGood;
	}
	if(strlen($username)>$Max_Strlen_UserName || strlen($username)<$Min_Strlen_UserName )
	{
		$UserNameGood="用户名长度不正确";
		return $UserNameGood;
	}
	if(!ereg($UserNameChars,$username))
	{
		$UserNameGood="用户名包含错误字符";
		return $UserNameGood;
	}
	return $UserNameGood;

}

function Check_Password($password1,$password2)
{
	$Max_Strlen_Password=16;
	$Min_Strlen_Password=6;
	$PasswordChars="^[a-zA-Z0-9_-]";
	$PasswordGood="用户密码正确";
	if($password1!=$password2)
	{
		$PasswordGood="前后密码不一致";
		return $PasswordGood;
	}
	if(strlen($password1)>$Max_Strlen_Password || strlen($password1)<$Min_Strlen_Password)
	{
		$PasswordGood="密码长度错误";
		return $PasswordGood;
	}
	if(!ereg($PasswordChars,$password1))
	{
		$PasswordGood="用户密码包含非法字符";
		return $PasswordGood;
	}
	return $PasswordGood;


}
function Check_Email($email)
{
	$EmailChars="^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$";//正则表达式
	$EmailGood="邮箱正确";
	if($email=="")
	{
		$EmailGood="邮箱不能为空";
		return $EmailGood;
	}
	if(!ereg($EmailChars,$email))
	{
		$EmailGood="邮箱地址包含非法字符";
		return $EmailGood;
	}
	$query="select * from tb_user_info where email='".$email."'";
	$result=mysql_query($query);
	$SameName=mysql_fetch_array($result);
	if($SameName["name"]!=NULL) //只要能查出一条来
	{
		$EmailGood="邮箱已经被注册";
	}
	return $EmailGood;

}

function actNum()
{	
	$chars_for_actnum=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
	"w","x","y","z","1","2","3","4","5","6","7","8","9","0"
	);

	for($i=0;$i<20;$i++)
	{
		$actnum.=$chars_for_actnum[mt_rand(0,count($chars_for_actnum)-1)];
	}
	
	return $actnum;

}

$query="select * from tb_user_info where name='".$username1."'";
$result=mysql_query($query);
$EmptySet=mysql_fetch_array($result);

if($EmptySet["name"]!=NULL)
{
	echo "用户名已经被注册";
}
else
{
	$UserNameGood=Check_Username($username1);
	$PasswordGood=Check_Password($password1,$password2);
	$EmailGood=Check_Email($email1);
	if( $UserNameGood=="用户名正确" && $PasswordGood=="用户密码正确" && $EmailGood=="邮箱正确" )
	{
		$query="insert into tb_user_info(name,password,email) values('".$username1."','".$password1."','".$email1."')";
		$result=mysql_query($query);
		if($result)
		{
			session_start();
			echo "注册成功";
			$to=$email1;
			$subject="激活码";
			$actnum=actNum();
			//$message="您的激活码为：$actnum";
			$header="From:929377310@qq.com";
			$mail=new PHPMailer();

			// 设置PHPMailer使用SMTP服务器发送Email
			$mail->IsSMTP();
			// 设置邮件的字符编码，若不指定，则为'UTF-8'
			$mail->CharSet='UTF-8';
			// 添加收件人地址，可以多次使用来添加多个收件人
			$mail->AddAddress($to);

			// 设置邮件正文
			$message=$username1.",您的激活地址为：http:\\localhost\svn_google\Imin\activate.php?actnum=".$actnum;
			$_SESSION["actnum"]=$actnum;
			$mail->Body=$message;

			// 设置邮件头的From字段。
			// 对于网易的SMTP服务，这部分必须和你的实际账号相同，否则会验证出错。
			$mail->From='929377310@qq.com';

			// 设置发件人名字
			$mail->FromName='Wang Jianhua';

			// 设置邮件标题
			$mail->Subject='Test Mail';

			// 设置SMTP服务器。这里使用网易的SMTP服务器。
			$mail->Host='smtp.qq.com';

			// 设置为“需要验证”
			$mail->SMTPAuth=true;

			// 设置用户名和密码，即网易邮件的用户名和密码。
			$mail->Username='929377310';
			$mail->Password='jhvohwdyfcaik';

			// 发送邮件。
			$mail->Send();
			echo "<br>";
			echo "激活码邮件已经发送";
			echo "<br>";
			$_SESSION['current_user']=$username1;
			echo "<a href='index.php'>返回首页</a>";

			

		}
	}
	else
	{
		if($UserNameGood!="用户名正确")
			echo $UserNameGood;
		if($PasswordGood!="用户密码正确")
			echo $PasswordGood;
		if($EmailGood!="邮箱正确")
			echo $EmailGood;
	}
}

?>